//
//  QBAlbumCell.m
//  QBImagePicker
//
//  Created by Katsuma Tanaka on 2015/04/06.
//  Copyright (c) 2015 Katsuma Tanaka. All rights reserved.
//

#import "SimAlbumCell.h"
#import "SImDefine.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "UIView+SimAdditions.h"

@interface SimAlbumCell()
{
    UIImageView *_assetImageView;
}

@end

@implementation SimAlbumCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _assetImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 55, 55)];
        _assetImageView.image = [self placeholderImageWithSize:_assetImageView.size];
        [self.contentView addSubview:_assetImageView];
        
        self.textLabel.font = [UIFont systemFontOfSize:16];
        self.textLabel.textColor = HEXRGBCOLOR(0x333333);
        
        self.detailTextLabel.font = [UIFont systemFontOfSize:14];
        self.detailTextLabel.textColor = HEXRGBCOLOR(0x808080);
        
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.bShowBottomDivideLine = YES;
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _assetImageView.frame = CGRectMake(0, 0, self.contentView.height, self.contentView.height);

    CGFloat left = CGRectGetMaxX(_assetImageView.frame) + 10;
    self.textLabel.left = left;
    [self.textLabel sizeToFit];
    [self.detailTextLabel sizeToFit];
    if (left + self.detailTextLabel.width + self.textLabel.width + 20 > self.contentView.width) {
        self.textLabel.width = self.contentView.width - (left + self.detailTextLabel.width - 20);
    }
    self.detailTextLabel.left = self.textLabel.right + 10;
    self.textLabel.centerY = self.detailTextLabel.centerY = self.contentView.height/2;
}

- (void)setAssetsGroup:(ALAssetsGroup *)assetsGroup
{
    NSUInteger numberOfAssets = MIN(1, [assetsGroup numberOfAssets]);
    if (numberOfAssets > 0) {
        NSRange range = NSMakeRange([assetsGroup numberOfAssets] - 1, 1);
        NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:range];
        [assetsGroup enumerateAssetsAtIndexes:indexes options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if (result) {
                UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
                _assetImageView.image = thumbnail;
                [self setNeedsLayout];
            }
        }];
    } else {
        UIImage *placeholderImage = [self placeholderImageWithSize:_assetImageView.size];
        _assetImageView.image = placeholderImage;
    }
    
    self.textLabel.text = [assetsGroup valueForProperty:ALAssetsGroupPropertyName];
    self.detailTextLabel.text = [NSString stringWithFormat:@"(%lu)", (long)assetsGroup.numberOfAssets];
}

- (UIImage *)placeholderImageWithSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    UIColor *backgroundColor = [UIColor colorWithRed:(239.0 / 255.0) green:(239.0 / 255.0) blue:(244.0 / 255.0) alpha:1.0];
    UIColor *iconColor = [UIColor colorWithRed:(179.0 / 255.0) green:(179.0 / 255.0) blue:(182.0 / 255.0) alpha:1.0];
    
    // Background
    CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    
    // Icon (back)
    CGRect backIconRect = CGRectMake(size.width * (16.0 / 68.0),
                                     size.height * (20.0 / 68.0),
                                     size.width * (32.0 / 68.0),
                                     size.height * (24.0 / 68.0));
    
    CGContextSetFillColorWithColor(context, [iconColor CGColor]);
    CGContextFillRect(context, backIconRect);
    
    CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
    CGContextFillRect(context, CGRectInset(backIconRect, 1.0, 1.0));
    
    // Icon (front)
    CGRect frontIconRect = CGRectMake(size.width * (20.0 / 68.0),
                                      size.height * (24.0 / 68.0),
                                      size.width * (32.0 / 68.0),
                                      size.height * (24.0 / 68.0));
    
    CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
    CGContextFillRect(context, CGRectInset(frontIconRect, -1.0, -1.0));
    
    CGContextSetFillColorWithColor(context, [iconColor CGColor]);
    CGContextFillRect(context, frontIconRect);
    
    CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
    CGContextFillRect(context, CGRectInset(frontIconRect, 1.0, 1.0));
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
